package com.hd.crdao.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.SmsCheckInfoBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.CheckUtils;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.TimeDownCounter;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/5/24 0024
 */
public class ForgetPwdActivity extends BaseSecondActivity {
    @BindView(R.id.forgetpwd_head)
    ExpandHeadView mForgetpwdHead;
    @BindView(R.id.telephone)
    EditText mTelephone;
    @BindView(R.id.getcheckcode)
    Button mGetcheckcode;
    @BindView(R.id.checkcode)
    EditText mCheckcode;
    @BindView(R.id.next)
    Button mNext;
    public TimeDownCounter mTimeDownCounter;
    public SmsCheckInfoBean mSmsCheckInfoBean;


    @Override
    protected void initView() {
        String dowhat = getIntent().getStringExtra("dowhat");
        if (TextUtils.equals(dowhat, "changePwd")) {
            mForgetpwdHead.getHeadViewMidText().setText("修改密码");
        } else {
            mForgetpwdHead.getHeadViewMidText().setText("忘记密码");
        }
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_forgetpwd;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mForgetpwdHead;
    }


    @OnClick({R.id.getcheckcode, R.id.next})
    public void onViewClicked(View view) {
        String phoneNumber = mTelephone.getText().toString().trim();
        switch (view.getId()) {
            case R.id.getcheckcode:
                if (CheckThePhoneNumber(phoneNumber)) {
                    getSmsCode(phoneNumber);
                }
                break;
            case R.id.next:     //首先去检验手机是否填写,并且判断手机是否被注册
                String checkCodeNum = mCheckcode.getText().toString().trim();
                CheckSmsCode(phoneNumber, checkCodeNum);
                break;
        }
    }


    /**
     * 检查验证码是否输入正确
     *
     * @param phoneNumber
     * @param checkCodeNum
     */
    private void CheckSmsCode(final String phoneNumber, String checkCodeNum) {
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", phoneNumber);
        map.put("smsCode", checkCodeNum);
        map.put("smsType", "2");        //校验忘记密码
        map.put("sign", Constants.SIGN);
        OkHttpUtils.post().url(Constants.CHECKSMSCODE_URL).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                SmsCheckInfoBean smsCheckInfoBean = new Gson().fromJson(response, SmsCheckInfoBean.class);
                if (smsCheckInfoBean.getSuccess() == 0) {
                    MyToast.show(smsCheckInfoBean.getMessage());
                    Intent intent = new Intent(UIUtils.getContext(), ChangeNewPwdActivity.class);
                    intent.putExtra("phoneNumber", phoneNumber);
                    intent.putExtra("dowhat","changePwd");
                    startActivity(intent);
                    finish();
                } else {
                    MyToast.show(smsCheckInfoBean.getMessage());
                }
            }
        });

    }

    /**
     * 检验手机号码
     */
    private boolean CheckThePhoneNumber(String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            MyToast.show("手机号码不能为空");
            return false;
        } else if (phoneNumber.length() == 11 && CheckUtils.isMobileNO(phoneNumber)) {
            return true;
        } else {
            MyToast.show("手机号码不合法");
            return false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimeDownCounter != null) {
            mTimeDownCounter.cancel();
            mTimeDownCounter = null;
        }
    }

    /**
     * 获取短信
     *
     * @param phoneNumber
     */
    public void getSmsCode(String phoneNumber) {
        OkHttpUtils.post().url(Constants.GETSMSCODE_URL)
                .addParams("mobile", phoneNumber)
                .addParams("smsType", "2")
                .addParams("sign", Constants.SIGN)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                mSmsCheckInfoBean = new Gson().fromJson(response, SmsCheckInfoBean.class);
                MyToast.show(mSmsCheckInfoBean.getMessage());
                if (mSmsCheckInfoBean.getSuccess() == 0) {
                    mTimeDownCounter = new TimeDownCounter(60000, 1000, mGetcheckcode);
                    mTimeDownCounter.start();
                } else {
                    MyToast.show(mSmsCheckInfoBean.getMessage());
                }
            }
        });
    }



}

